//
//  DIYListVC.m
//  BigJoy
//
//  Created by Rowling on 2017/7/18.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "DIYListVC.h"
#import "DIYHomeCell.h"
#import "DIYDetailViewController.h"

@interface DIYListVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    NSInteger _currentIndex;
    CGFloat _dragStartX;
    CGFloat _dragEndX;
}
@property (nonatomic,strong) UICollectionView * collectionView;
@end

@implementation DIYListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseMutableDataArray = [NSMutableArray arrayWithArray:@[@"test1.jpg",@"jay.jpeg",@"1.jpeg",@"2.jpeg",@"test1.jpg",@"jay.jpeg",@"1.jpeg",@"2.jpeg"]];
    [self setUI];
}

-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(XMW(300),XMW(400));
        layout.minimumLineSpacing  = (0.2 * SCREEN_WIDTH ) / 4;
        layout.sectionInset = UIEdgeInsetsMake(0, (SCREEN_WIDTH - XMW(300)) / 2, 10, (SCREEN_WIDTH - XMW(300)) / 2);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, XMW(200), SCREEN_WIDTH,XMW(400) + 20) collectionViewLayout:layout];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([DIYHomeCell class]) bundle:nil] forCellWithReuseIdentifier:@"Cell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(void)setUI {
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView * headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, XMW(300))];
    [self.view addSubview:headImageView];
    headImageView.image = [UIImage imageNamed:@"1.jpeg"];
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 50, 50)];
    [backButton setImage:[UIImage imageNamed:@"fanhuibaiyouyiny"] forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    @weakify(self);
    [[backButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self.view addSubview:self.collectionView];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.baseMutableDataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DIYHomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:self.baseMutableDataArray[indexPath.row]];
    cell.isList = YES;
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DIYDetailViewController * vc = [DIYDetailViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)scrollToCenter
{
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

//手指拖动开始
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _dragStartX = scrollView.contentOffset.x;
}

//手指拖动停止
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    _dragEndX = scrollView.contentOffset.x;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self fixCellToCenter];
    });
}

//配置cell居中
-(void)fixCellToCenter
{
    //最小滚动距离
    float dragMiniDistance = SCREEN_WIDTH/20.0f;
    if (_dragStartX -  _dragEndX >= dragMiniDistance) {
        _currentIndex -= 1;//向右
    }else if(_dragEndX -  _dragStartX >= dragMiniDistance){
        _currentIndex += 1;//向左
    }
    NSInteger maxIndex = [_collectionView numberOfItemsInSection:0] - 1;
    _currentIndex = _currentIndex <= 0 ? 0 : _currentIndex;
    _currentIndex = _currentIndex >= maxIndex ? maxIndex : _currentIndex;
    [self scrollToCenter];
    NSLog(@"%ld",_currentIndex);
}
@end
